package ran.youling.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

/**
 * ran
 * 封装redis简化对过期时间的操作
 */
@Component
public class RedisUtil {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    // 设置缓存过期时间
    public boolean expire(String key, long time) {
        return redisTemplate.expire(key, time, TimeUnit.SECONDS);
    }

    // 获取缓存值
    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    // 设置缓存值
    public void set(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    // 删除缓存
    public void del(String... keys) {
        if (keys != null && keys.length > 0) {
            if (keys.length == 1) {
                redisTemplate.delete(keys[0]);
            } else {
                redisTemplate.delete(Arrays.asList(keys));
            }
        }
    }
// 其他操作
}